package cn.xclink.kernel.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xclink.common.web.BaseController;
import cn.xclink.common.web.ExecuteResult;
import cn.xclink.common.web.bind.annotation.CurrentUser;
import cn.xclink.kernel.entity.Resource;
import cn.xclink.kernel.entity.User;
import cn.xclink.kernel.service.ResourceService;

@Controller
@RequestMapping("system/resource")
public class ResourceController extends BaseController<Resource>{

	public static String LEFT_DIVID = "sys_resource_left_div";
	public static String RIGHT_DIVID = "sys_resource_right_div";
	
	@Autowired
	private ResourceService resourceService;
	
	@ModelAttribute
	public void init(Model model) {
		model.addAttribute("left_divid", LEFT_DIVID);
		model.addAttribute("right_divid", RIGHT_DIVID);
	}
	
	@RequiresPermissions("resource:view")
	@RequestMapping
	public String index(@CurrentUser User currentUser, Model model, HttpServletRequest request) {
		model.addAttribute("resourceList", resourceService.findModelsAndMenus());
		return "system/resource/index";
	}
	
	@RequiresPermissions("resource:view")
	@RequestMapping(value = "/list")
	public String list(@CurrentUser User currentUser, Resource resource, Model model,HttpServletRequest request,
			@RequestParam(value="pageNum",required=false) Integer pageNum, 
    		@RequestParam(value="pageSize",required=false) Integer pageSize) {
		model.addAttribute("pageInfo", resourceService.findPageInfo(resource, pageNum, pageSize));
		model.addAttribute("resource", resource);
		return "system/resource/list";
	}
	
	@RequiresPermissions("resource:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String createForm(@CurrentUser User currentUser,Resource resource, Model model,HttpServletRequest request) {
		Resource parent = resourceService.get(resource.getParentId());
		model.addAttribute("parent", parent);
		resource.setParentIds(parent.makeSelfAsParentIds());
		model.addAttribute("resource", resource);
		return "system/resource/create";
	}
	

	@RequiresPermissions("resource:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(Resource resource) {
		try {
			Integer result = resourceService.save(resource);
			if (result == 1) {
				return ExecuteResult.jsonReturn(200,LEFT_DIVID + "," + RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}

	@RequiresPermissions("resource:edit")
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String editForm(@CurrentUser User currentUser,@PathVariable("id") Long id, Model model) {
		Resource resource  = resourceService.get(id);
		model.addAttribute("resource", resource);
		Resource parent = resourceService.get(resource.getParentId());
		model.addAttribute("parent", parent);
		return "system/resource/edit";
	}

	@RequiresPermissions("resource:edit")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(Resource resource) {
		try {
			Integer result = resourceService.updateNotNull(resource);
			if (result == 1) {
				return ExecuteResult.jsonReturn(200,LEFT_DIVID + "," + RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}

	@RequiresPermissions("resource:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@PathVariable("id") Long id,HttpServletRequest request) {
		try {
			Integer result = resourceService.delete(id);
			if (result == 1) {
				return ExecuteResult.jsonReturn(200,LEFT_DIVID + "," + RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}
}
